// Author: Bjorn Schobben

#include "ColorButton.h"
#include "PaintPaletteWidget.h"

PaintPaletteWidget::PaintPaletteWidget(QWidget *parent, const Properties& properties) : QWidget(parent), gridLayout(0), frame(0), vboxLayout(0)
{
	// Setup the ui of the paint palette widget
	SetupUi(properties);
}

PaintPaletteWidget::~PaintPaletteWidget()
{
}

void PaintPaletteWidget::SetupUi(const Properties& properties)
{
	setMaximumWidth(80);
	setMinimumWidth(80);
	setMinimumHeight(200);

    gridLayout = new QGridLayout(this);
    gridLayout->setSpacing(6);
    gridLayout->setMargin(9);
    frame = new QFrame(this);
    frame->setFrameShape(QFrame::Box);
    frame->setFrameShadow(QFrame::Sunken);
	
    vboxLayout = new QVBoxLayout(frame);
    vboxLayout->setSpacing(6);
    vboxLayout->setMargin(9);

	SetupColorButtons(properties);

    gridLayout->addWidget(frame, 0, 0, 1, 1);
}

void PaintPaletteWidget::SetupColorButtons(const Properties& properties)
{
    ColorButton *colorButton;


	// Setup the color buttons	
	for(unsigned int i=0; i<properties.GetColorCount()+1; i++)
	{
	    colorButton = new ColorButton(frame);

		// If all colors have been added, then we can add the eraser button
		if (i == properties.GetColorCount())
		{
			// Set color to the erased color
			colorButton->EraseColor();
		}
		else
		{
			// Set color to the properties color
			colorButton->SetColor(properties.GetColor(i));
		}

	    QSizePolicy sizePolicy(static_cast<QSizePolicy::Policy>(7), static_cast<QSizePolicy::Policy>(7));
		colorButton->setSizePolicy(sizePolicy);

		vboxLayout->addWidget(colorButton);

		// Connect the clicked signal
		QObject::connect(colorButton, SIGNAL(clicked(ColorButton*, Color)), this, SLOT(SelectedColor(ColorButton*, Color)));
	}
}

// Color the image with a new color
void PaintPaletteWidget::SetImageColor(QImage& image, Color color)
{
	for(unsigned int y = 0; y < image.height(); y++)
	{
		for(unsigned int x = 0; x < image.width(); x++)
		{
			// Get image's pixel data
			QColor pixel = QColor::fromRgba(image.pixel(x,y));

			// If pixel is white
			if (pixel == Qt::white)
			{
				// Change the pixel to the selected color
				QColor newPixel = QColor(color.GetRed(), color.GetGreen(), color.GetBlue());

				image.setPixel(x,y, newPixel.rgba());
			}
		}
	}
}

// Set the palette's new main color
void PaintPaletteWidget::SetPaletteColor(Color color)
{
	// Send a signal to notify others that the color changed
	emit colorChanged(color);

	// Now we start to create our mouse cursor brush
	QCursor cursor;
	
	// If selected color is not erased
	if (color.IsColor())
	{
		// Load initial brush image
		QImage image("icons/brush.png");

		// Change the white portions to the selected color
		SetImageColor(image, color);

		// Create a new cursor based on the image, and set its hotspot
		cursor = QCursor(QPixmap::fromImage(image), 0, image.height()-1);
	}
	else
	{
		// Load initial eraser image
		QImage image("icons/eraser.png");

		// Create a new cursor based on the image, and set its hotspot
		cursor = QCursor(QPixmap::fromImage(image), image.width()/2, image.height()/2);
	}
	
	// Notify listeners that the cursor changed
	emit cursorChanged(cursor);
}

//Slot for when a button changes color
void PaintPaletteWidget::SelectedColor(ColorButton* button, Color color)
{
	// Set the palette's main color
	SetPaletteColor(color);
}


